<template>
	<view class="main"></view>
</template>

<script>
	export default {
		data() {
			return {
				appInfo: null,
			}
		},
		onLoad(option) {
			console.log("ready onLoad", option)
		},
		mounted() {
			console.log("ready mounted")
			this.initData();
		},
		methods: {
			initData() {
				console.log("ready initData")
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (appInfo) => {
					this.appInfo = appInfo;
					console.log("appInfo", this.appInfo);

					this.$store.commit("updateVersion", {
						app_version: plus.runtime.version,
						app_versionCode: plus.runtime.versionCode,
						wgt_version: this.appInfo.version,
						wgt_versionCode: this.appInfo.versionCode,
					});
					// if (process.env.NODE_ENV == "development") {
					// 	this.getSession();
					// } else {
					this.getLatestAppVersion();
					// }
				})
				// #endif

				// #ifdef H5
				this.getSession();
				// #endif

			},
			getLatestAppVersion() {
				console.log("getLatestAppVersion")
				this.axios
					.get("/api/services/app/Version/GetLatestVersion", {
						params: {
							type: "appLink"
						}
					})
					.then((data) => {
						console.log("getLatestAppVersion", data)

						// 云端有更高版本
						if (data.versionCode > this.appInfo.versionCode) {
							uni.showModal({
								title: '提示',
								content: `检测到新版本 ${data.version}，即将开始自动更新。`,
								cancelColor: '#666666',
								confirmColor: '#666666',
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										this.showLoading("正在下载...");
										var dtask = plus.downloader.createDownload(data.appLink, {},
											(d, status) => {
												// 下载完成  
												if (status == 200) {
													plus.runtime.install(plus.io
														.convertLocalFileSystemURL(d
															.filename), {}, {},
														(error) => {
															uni.showModal({
																title: '提示',
																content: '自动安装失败，请手动下载最新版本',
																cancelColor: '#666666',
																confirmColor: '#666666',
																showCancel: false,
																success: (res) => {
																	plus.runtime
																		.openURL(
																			data
																			.appLink,
																			() => {
																				console
																					.log(
																						"plus.runtime.openURL"
																					);
																			});
																},
															})
														})
												} else {
													plus.downloader.clear();
													uni.showModal({
														title: '提示',
														content: '自动更新失败，请手动下载最新版本',
														cancelColor: '#666666',
														confirmColor: '#666666',
														showCancel: false,
														success: (res) => {
															plus.runtime
																.openURL(
																	data.appLink,
																	() => {
																		console.log(
																			"plus.runtime.openURL"
																		);
																	});
														},
													})
												}
											});
										dtask.start();
									}
								},
							})

						} else {
							console.log("已经是最新APP版本")
							this.getLatestWgtVersion();
						}
					});
			},
			getLatestWgtVersion() {
				console.log("getLatestWgtVersion")
				this.axios
					.get("/api/services/app/Version/GetLatestVersion", {
						params: {
							type: "wgtLink"
						}
					})
					.then((data) => {
						if (data.versionCode > this.appInfo.versionCode) {

							console.log("getLatestWgtVersion", data)
							this.showLoading("数据更新中");
							uni.downloadFile({
								url: data.wgtLink,
								success: (downloadResult) => {
									this.hideLoading();
									if (downloadResult.statusCode === 200) {
										this.showLoading("数据加载中");
										console.log("downloadResult", downloadResult)
										plus.runtime.install(
											downloadResult
											.tempFilePath, {
												force: true
											},
											() => {
												console.log('wgt安装成功 重启应用');
												this.hideLoading();
												plus.runtime
													.restart();
											},
											(e) => {
												console.log("数据加载失败", e)
												this.hideLoading();
												uni.showToast({
													title: '数据加载失败',
													icon: 'none',
												});
												this.getSession();
											});
									} else {
										uni.showToast({
											title: '数据更新失败',
											icon: 'none',
										});
									}
								},
								fail: () => {
									uni.showToast({
										title: '数据更新失败',
										icon: 'none',
									});
								}
							})
						} else {
							console.log("已经是最新WGT版本");
							this.getSession();
						}
					})
			},
			getSession() {

				// #ifdef APP-PLUS
				//保持屏幕常亮
				uni.setKeepScreenOn({
					keepScreenOn: true
				});
				//申请电池白名单（app保活。防止悬浮窗因app被关闭而关闭）
				this.$XFModule.requestIgnoreBattery((ret) => {
					console.log("================requestIgnoreBattery================", ret.status)
					if (ret.status) {
						//悬浮窗权限申请
						const XFModule_FloatPermission = this.$XFModule.checkFloatPermission()
						console.log("================checkFloatPermission================",
							XFModule_FloatPermission)
						this.$store.commit("setXFModule_FloatPermission", XFModule_FloatPermission)

					}
				})
				// #endif


				if (this.$store.state.session.token) {
					this.axios.get("/api/services/app/Task/GetPersonInfo").then(res => {
						this.$store.setSession("schoolId", res.schoolId)
						this.$store.setSession("schoolName", res.schoolName)
						this.$store.setSession("classId", res.classId)
						this.$store.setSession("className", res.className)
						this.$store.setSession("userId", res.userId)
						this.$store.setSession("userName", res.userName)

						uni.reLaunch({
							url: '/pages/index/index'
						});
					})
				} else {
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}

				//隐藏之前版本更新时可能未消除的toast
				uni.hideToast();

				// #ifdef APP-PLUS
				plus.navigator.closeSplashscreen();
				// #endif
			},

		}
	}
</script>

<style lang="scss">


</style>